#include "music.h"
#include<QUuid>


Music::Music():_isLike(false),_isHistory(false){}

Music::Music(const QUrl& url)
    :_isLike(false)
    ,_isHistory(false)
    ,_musicUrl(url)
{
    _musicId = QUuid::createUuid().toString();
    parseMediaMetaData();
}

void Music::setIsLike(bool isLike)
{
    _isLike = isLike;
}

void Music::setIsHistory(bool isHistory)
{
    _isHistory = isHistory;
}

void Music::setMusicName(const QString &musicName)
{
    _musicName = musicName;
}

void Music::setSingerName(const QString &singerName)
{
    _singerName = singerName;
}

void Music::setAlbumName(const QString &albumName)
{
    _albumName = albumName;
}

void Music::setDuration(const qint64 duration)
{
    _duration = duration;
}

void Music::setMusicUrl(const QUrl &url)
{
    _musicUrl = url;
}

void Music::setMusicId(const QString &musicId)
{
    _musicId = musicId;
}

bool Music::getIsLike()const
{
    return _isLike;
}

bool Music::getIsHistory()const
{
    return _isHistory;
}

QString Music::getMusicName()const
{
    return _musicName;
}

QString Music::getSingerName() const
{
    return _singerName;
}

QString Music::getAlbumName() const
{
    return _albumName;
}

qint64 Music::getDuration() const
{
    return _duration;
}

QUrl Music::getMusicUrl() const
{
    return _musicUrl;
}

QString Music::getMusicId() const
{
    return _musicId;
}

QString Music::getLrcFilePath() const
{
   //qDebug() << _musicUrl;
    //QString path = _musicUrl.toString();
    QString path = _musicUrl.toLocalFile();
    //qDebug() << path;
    path.replace(".mp3", ".lrc");
    path.replace(".flac", ".lrc");
    path.replace(".mpga", ".lrc");
    return path;
}

void Music::parseMediaMetaData()
{

    QMediaPlayer player;
    //qDebug() << "nihao";

    // 媒体元数据解析需要时间，只有等待解析完成之后，才能提取⾳乐信息
    // 当元数据加载完成以后，会发送metadataChanged信号，此时就可以处理我们需要的内容了
    // 连接元数据变化信号
    QObject::connect(&player, &QMediaPlayer::metaDataChanged, [&]() {
        // 提取元数据
        _musicName = player.metaData().value(QMediaMetaData::Title).toString();
        _singerName = player.metaData().value(QMediaMetaData::Author).toString();
        _albumName = player.metaData().value(QMediaMetaData::AlbumTitle).toString();
        _duration = player.duration();

        QString fileName =  _musicUrl.toString();
        int i = fileName.size() - 1;
        for(; i >= 0; --i){
            if(fileName[i] == '/')break;
        }

        int index = fileName.indexOf("-");
        if(_musicName.isEmpty())
        {
            if(index != -1)
            {
                // "动物世界 - 薛之谦.mp3"
                _musicName = fileName.mid(i + 1, index - i - 1).trimmed();
            }
            else
            {
                // "动物世界.mp3"
                _musicName = fileName.mid(i + 1, fileName.indexOf('.') - i - 1).trimmed();
            }
        }
        // 作者为空
        if(_singerName.isEmpty())
        {
            if(index != -1)
            {
                _singerName = fileName.mid(index+1, fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                _singerName = "未知歌⼿";
            }
        }
        if(_albumName.isEmpty()){_albumName = "未知专辑";}

        // // 打印结果
        // qDebug() << "歌曲名称:" << _musicName;
        // qDebug() << "歌手:" << getSingerName();
        // qDebug() << "专辑:" << _albumName;
        // qDebug() << "时长:" << _duration << "毫秒";
    });

    // 解析时候需要读取歌曲数据，读取歌曲⽂件需要⽤到QMediaPlayer类
    //加载歌曲文件
    player.setSource(_musicUrl);




}
